<?php

namespace App\Http\Controllers;

use App\Exports\CategoryExport;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;

class CategoryController extends Controller
{
    public function index(Request $request)
    {
        return view('category');
    }

    public function list(Request $request)
    {
        $perPage = $request->input('limit', 10);
        $categories = Category::query()->orderBy('id')
            ->paginate($perPage);
        return $this->paginate('', $categories);
    }

    public function store(Request $request)
    {
        // 必填项
        $validator = Validator::make($request->input(), [
            'title' => 'required',
        ], [
            'title.required' => '名称不能为空',
        ]);
        if ($validator->stopOnFirstFailure()->fails()) {
            $errors = $validator->errors();
            return $this->error($errors->first());
        }
        $title = $request->input('title');
        $categoryExist = Category::query()->where('title', $title)
            ->first();
        if ($categoryExist) {
            return $this->error('名称已存在，不能重复添加');
        }
        $category = new Category();
        $category->title = $title;
        $category->save();
        return $this->ok('保存成功');
    }

    public function update(Request $request)
    {
        // 必填项
        $validator = Validator::make($request->input(), [
            'title' => 'required',
            'id' => 'required',
        ], [
            'title.required' => '名称不能为空',
        ]);
        if ($validator->stopOnFirstFailure()->fails()) {
            $errors = $validator->errors();
            return $this->error($errors->first());
        }
        $title = $request->input('title');
        $id = $request->input('id');
        $category = Category::query()->where('id', $id)
            ->first();
        if (!$category) {
            return $this->error('分类不存在');
        }
        $categoryExist = Category::query()->where('title', $title)
            ->where('id', '<>', $id)
            ->first();
        if ($categoryExist) {
            return $this->error('名称已存在，不能重复添加');
        }
        $category->title = $title;
        $category->save();
        return $this->ok('保存成功');
    }

    public function destroy(Request $request)
    {
        $id = $request->input('id');
        if (!$id) {
            return $this->error('请选择要删除的分类');
        }
        $category = Category::query()->where('id', $id)
            ->first();
        if (!$category) {
            return $this->error('请选择要删除的分类');
        }
        $category->delete();
        return $this->ok('删除成功');
    }

    public function export(Request $request)
    {
        $categories = Category::query()->orderBy('id')
            ->get()
            ->toArray();
//        return Excel::download(new CategoryExport($categories), '分类.xls');

        $fileName = '分类.xlsx';
        $filePath = public_path('storage/' . $fileName);
        Excel::store(new CategoryExport($categories), $fileName);
        // 压缩包
        $zip = new \ZipArchive();
        $zipFilename = '分类.zip';
        if ($zip->open($zipFilename, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) === TRUE) {
            $zip->addFile($filePath, $fileName);
            $zip->close();

            // 删除临时文件
            unlink($filePath);
            return response()->download($zipFilename);
        }
        return response('导出失败');
    }
}
